package ciyuan.Block.Machine.dlb;

import ciyuan.Block.TypeInterface.Machine;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import net.minecraftforge.fml.network.NetworkHooks;

import javax.annotation.Nullable;

public class Dlb extends Machine {

    private DlbTileEntity tileEntity;

    @Nullable
    @Override
    public TileEntity createTileEntity(BlockState state, IBlockReader world) {
        tileEntity = new DlbTileEntity();
        return tileEntity;
    }

    @Override
    public boolean hasTileEntity(BlockState Anvilate) {
        return tileEntity != null;
    }

    @Override
    public void destroy(IWorld world, BlockPos blockPos, BlockState blockState) {
        this.tileEntity = null;
    }

    @Override
    public ActionResultType use(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
        if (!worldIn.isClientSide() && handIn == Hand.MAIN_HAND) {
            if (!this.tileEntity.hasLevel()) {
                this.tileEntity.setLevelAndPosition(worldIn, pos);
            }
            NetworkHooks.openGui((ServerPlayerEntity) player, this.tileEntity, (PacketBuffer packerBuffer) -> {
                packerBuffer.writeBlockPos(this.tileEntity.getBlockPos());
            });
        }
        return ActionResultType.SUCCESS;
    }

    @Override
    public boolean hasItem() {
        return !tileEntity.getInventory().isEmpty();
    }
    
}
